package com.example.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.enums.ResultCodeEnum;
import com.example.entity.College;
import com.example.entity.ZyzActivity;
import com.example.entity.ZyzSignUp;
import com.example.entity.ZyzStudent;
import com.example.exception.CustomException;
import com.example.mapper.CollegeMapper;
import com.example.mapper.ZyzActivityMapper;
import com.example.mapper.ZyzSignUpMapper;
import com.example.mapper.ZyzStudentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报名服务
 */
@Service
public class ZyzSignUpService {
    @Autowired
    private ZyzSignUpMapper zyzSignUpMapper;
    @Autowired
    private ZyzStudentMapper zyzStudentMapper;
    @Autowired
    private ZyzActivityMapper zyzActivityMapper;
    @Autowired
    private CollegeMapper collegeMapper;

    /**
     * 报名(添加)
     * @param zyzSignUp
     */
    public void add(ZyzSignUp zyzSignUp) {
        ZyzSignUp zyzSignUp1 =zyzSignUpMapper.selectByStudentIdAndActivityId(zyzSignUp.getStudentId(), zyzSignUp.getActivityId());
        if (ObjectUtil.isNotEmpty(zyzSignUp1)){
            throw new CustomException(ResultCodeEnum.SIGN_UP_ERROR);
        }
        //根据学生id查询学生所在的学院
        ZyzStudent zyzStudent = zyzStudentMapper.selectById(zyzSignUp.getStudentId());
        if (ObjectUtil.isNotEmpty(zyzStudent)){
            Integer collegeId = zyzStudent.getCollegeId();
            //根据活动id查询活动
            ZyzActivity zyzActivity = zyzActivityMapper.selectById(zyzSignUp.getActivityId());
            //根据学院id查询学院
            College college = collegeMapper.selectById(collegeId);
            String name = college.getName();
            if (ObjectUtil.isNotEmpty(zyzActivity)){
                String collegeIds = zyzActivity.getCollegeIds();
                if (ObjectUtil.isEmpty(collegeIds)){
                    zyzSignUp.setAddTime(new Date());
                    zyzSignUpMapper.insert(zyzSignUp);
                }else{
                    if (collegeIds.contains(collegeId.toString())){
                        zyzSignUp.setAddTime(new Date());
                        zyzSignUpMapper.insert(zyzSignUp);
                    }else {
                        throw new CustomException(ResultCodeEnum.ACTIVITY_ERROR);
                    }
                }
            }
        }


    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    public int deleteBatch(String ids) {
        return zyzSignUpMapper.deleteBatch(Convert.toStrArray(ids));

    }

    /**
     * 查询所有
     * @param zyzSignUp
     * @return
     */
    public List<Map<String, Object>> selectAll(ZyzSignUp zyzSignUp) {
        List<Map<String, Object>> maps = zyzSignUpMapper.selectAll(zyzSignUp);

        return maps;
    }

    /**
     * 分页查询
     * @param zyzSignUp
     * @param pageNum
     * @param pageSize
     * @return
     */
    public  PageInfo<Map<String, Object>> selectPage(ZyzSignUp zyzSignUp, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> maps = zyzSignUpMapper.selectAll(zyzSignUp);
        return PageInfo.of(maps);
    }

    /**
     * 查询积分和参与时长
     * @param studentId
     * @return
     */
    public Map<String, Object> selectPoints(Integer studentId) {
        Map<String, Object> map = new HashMap<>();
        List<ZyzSignUp> list = zyzSignUpMapper.selectByStudentId(studentId);
        Double points = 0.0;
        Double duration = 0.0;
        if (ObjectUtil.isNotEmpty(list)){
            for (ZyzSignUp zyzSignUp : list) {
                ZyzActivity zyzActivity = zyzActivityMapper.selectById(zyzSignUp.getActivityId());
                points += zyzActivity.getPoints();
                duration += zyzSignUp.getDuration();
            }
        }
        ZyzStudent zyzStudent = zyzStudentMapper.selectById(studentId);
        if (ObjectUtil.isNotEmpty(zyzStudent)){
            Integer idCord = zyzStudent.getIdCord();
            String name = zyzStudent.getName();
            String avatar = zyzStudent.getAvatar();
            map.put("idCord", idCord);
            map.put("name", name);
            map.put("avatar", avatar);
        }
        //查询参与次数
        Integer count = zyzSignUpMapper.countByStudentId(studentId);
        map.put("count", count);
        map.put("points", points);
        map.put("duration", duration);
        return map;
    }

    /**
     * 查询通知
     * @param studentId
     * @return
     */
    public List<ZyzSignUp> selectNotice(Integer studentId) {
        List<ZyzSignUp> list = zyzSignUpMapper.selectNotice(studentId);
        return list;
    }


    public List<Map<String, Object>> selectStudentActivity(Integer studentId) {
        return zyzSignUpMapper.selectStudentActivity(studentId);
    }
}
